package com.studyyoun.android.nonghe.home.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.androidlongs.pullrefreshrecyclerylib.common.FullyLinearLayoutManager;
import com.studyyoun.android.androidcommonbaselibrary.activity.CommonBaseActivity;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.model.HomeMsgMainModel;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by androidlongs on 2017/11/20.
 */

public class MoreHomeItemActivity extends CommonBaseActivity {

    private TextView mTitleTextView;
    private RecyclerView mContentRecyvlerView;

    @Override
    protected void getAllIntentExtraDatas(Intent intent) {

    }

    @Override
    protected int getCommonLayoutId() {
        return R.layout.activity_item_more_layout;
    }

    @Override
    protected void commonInitView(View view) {

        mTitleTextView = findViewById(R.id.tv_base_header_title);

        mContentRecyvlerView = findViewById(R.id.rv_item_more_content);
    }

    @Override
    protected void commonFunction() {

        mTitleTextView.setText("公告");
        findViewById(R.id.ll_base_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MoreHomeItemActivity.this.finish();
            }
        });
    }

    private List<HomeMsgMainModel.DataBean> mDataBeanList = new ArrayList<>();

    @Override
    protected void commonDelayFunction() {

        String url = HttpUrlHelper.sGetMoreAllClassUrl;

        ConcurrentHashMap<String, String> key = new ConcurrentHashMap<>();
        key.put("pageIndex", "1");
        key.put("pageSize", "200");

        OkhttpCommonClient.sentPostRequest(url, key, new OkhttpCommonCallBack(HomeMsgMainModel.class) {
            @Override
            public void onSuccess(Object obj) {
                if (obj != null) {
                    HomeMsgMainModel lHomeMsgMainModel = (HomeMsgMainModel) obj;
                    List<HomeMsgMainModel.DataBean> lData = lHomeMsgMainModel.getData();
                    if (lData != null) {
                        mDataBeanList = lData;
                        setMsgDataFunction();
                    } else {
                        ToastUtils.show("暂无最新公告 ", mContext);
                    }
                } else {
                    ToastUtils.show("暂无最新公告 ", mContext);
                }

            }

            @Override
            public void onFaile(Object obj) {
                ToastUtils.show("暂无最新公告 ", mContext);
            }
        });
    }


    private HomeMsgDataAdapter mHomeMsgDataAdapter;

    private void setMsgDataFunction() {
        if (mHomeMsgDataAdapter == null) {
            mHomeMsgDataAdapter = new HomeMsgDataAdapter(mContext, mDataBeanList);
            mContentRecyvlerView.setLayoutManager(new FullyLinearLayoutManager(mContext));
            mContentRecyvlerView.setAdapter(mHomeMsgDataAdapter);
        } else {
            mHomeMsgDataAdapter.setListDatas(mDataBeanList);
            mHomeMsgDataAdapter.notifyDataSetChanged();
        }


    }


    public class HomeMsgDataAdapter extends RecyclerView.Adapter<HomeMsgDataHolder> {

        private Context mContext;

        public HomeMsgDataAdapter(Context pContext, List<HomeMsgMainModel.DataBean> pDataBeanList) {
            mDataBeanList = pDataBeanList;
            mContext = pContext;

        }

        @Override
        public HomeMsgDataHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View lView = View.inflate(mContext, R.layout.home_item_layout_2, null);
            return new HomeMsgDataHolder(lView, mContext);
        }

        @Override
        public void onBindViewHolder(HomeMsgDataHolder holder, int position) {

            HomeMsgMainModel.DataBean lDataBean = mDataBeanList.get(position);
            holder.setDatas(lDataBean, position);
        }

        @Override
        public int getItemCount() {
            return mDataBeanList == null ? 0 : mDataBeanList.size();
        }

        private List<HomeMsgMainModel.DataBean> mDataBeanList;

        public void setListDatas(List<HomeMsgMainModel.DataBean> pListDatas) {
            mDataBeanList = pListDatas;
        }
    }


    public class HomeMsgDataHolder extends RecyclerView.ViewHolder {

        private final TextView mItemTextView;
        private final TextView mItemTimeTextView;

        public HomeMsgDataHolder(View itemView, Context pContext) {
            super(itemView);
            mItemTextView = itemView.findViewById(R.id.tv_item_title);
            mItemTimeTextView = itemView.findViewById(R.id.tv_item_time);
        }

        public void setDatas(final HomeMsgMainModel.DataBean pDataBean, int pPosition) {

            String lTitle = pDataBean.getTitle();
            if (TextUtils.isEmpty(lTitle)) {
                lTitle = "***";
            }
            mItemTextView.setText(lTitle);

            String lAddTime = pDataBean.getAddTime();
            if (TextUtils.isEmpty(lAddTime)) {
                lAddTime = "***";
            }
            mItemTimeTextView.setText(lAddTime);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent lIntent = new Intent(mContext, HomeMsgItemDesActivity.class);
                    lIntent.putExtra("model", pDataBean);
                    lIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(lIntent);
                }
            });
        }
    }
}
